using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MMXSaveGameConverter
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            SaveGameList.MouseDoubleClick += OnSaveGameListMouseDoubleClick;
            SaveGameList.SelectionChanged += OnSaveGameListSelectionChanged;
            SaveGameList.Drop += OnSaveGameListDrop;   
            Invoke(() => SaveGameList.ItemsSource = new SaveGameViewItems(Dispatcher));
        }

        private void OnSaveGameListDrop(object sender, DragEventArgs e)
        {
            try
            {
                ((SaveGameViewItems)SaveGameList.ItemsSource).TryAdd((string[])e.Data.GetData(DataFormats.FileDrop));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void OnSaveGameListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            HashSet<SaveGameViewItem> toRemove = new HashSet<SaveGameViewItem>();
            try
            {
                foreach (SaveGameViewItem item in e.RemovedItems.Cast<SaveGameViewItem>())
                {
                    try
                    {
                        item.OnDeselect();
                    }
                    catch
                    {
                        toRemove.Add(item);
                    }
                }

                foreach (SaveGameViewItem item in e.AddedItems.Cast<SaveGameViewItem>())
                {
                    try
                    {
                        item.OnSelect();
                        SaveGameView.DataContext = item;
                    }
                    catch
                    {
                        SaveGameView.DataContext = null;
                        toRemove.Add(item);
                    }
                }

                foreach (SaveGameViewItem item in toRemove)
                    ((SaveGameViewItems)SaveGameList.ItemsSource).Remove(item);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void OnSaveGameListMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                SaveGameViewItem item = SaveGameList.SelectedItem as SaveGameViewItem;
                if (item == null)
                    return;

                Process.Start("explorer", String.Format("/select, \"{0}\"", item.FullPath));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Invoke(Action action)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                try
                {
                    action();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                    Environment.Exit(1);
                }
            }));
        }
    }
}
